//
//  ViewController.m
//  TigerClef
//
//  Created by Christopher Laguna on 6/28/13.
//  Copyright (c) 2013 Christopher Laguna. All rights reserved.
//

#import "ViewController.h"
#import "AppDelegate.h"
#import "LevelSelectViewController.h"

int levelsBeat = 0;

@interface ViewController ()

@end

@implementation ViewController
@synthesize staffView;
@synthesize topView;
@synthesize page;
@synthesize tutorialText;
@synthesize tutorialTextArray;
@synthesize nextButton;
@synthesize buttons;
@synthesize button1;
@synthesize button2;
@synthesize button3;
@synthesize button4;
@synthesize button5;
@synthesize button6;
@synthesize button7;
@synthesize treblePitches;
@synthesize bassPitches;
@synthesize altoPitches;
@synthesize fancyPitches1;
@synthesize fancyPitches2;
@synthesize fancyPitches3;
@synthesize fancyPitches4;
@synthesize fancyPitches5;
@synthesize currentAnswer;
@synthesize numCorrectAnswers;
@synthesize numTotalAnswers;
@synthesize firstAnswer;
@synthesize level;
@synthesize tenorPitches;
@synthesize cleffPositionfromGenerateAnswer;

- (id) initWithLevel: (int) aLevel {
    
    level = aLevel;
    
    self = [super init];
    
    treblePitches   = [[NSMutableArray alloc] initWithObjects:@"E",@"F",@"G",@"A",@"B",@"C",@"D",nil];
    bassPitches     = [[NSMutableArray alloc] initWithObjects:@"G",@"A",@"B",@"C",@"D",@"E",@"F",nil];
    altoPitches     = [[NSMutableArray alloc] initWithObjects:@"F",@"G",@"A",@"B",@"C",@"D",@"E",nil];
    tenorPitches    = [[NSMutableArray alloc] initWithObjects:@"D",@"E",@"F",@"G",@"A",@"B",@"C",nil];
    fancyPitches1   = [[NSMutableArray alloc] initWithObjects:@"C",@"D",@"E",@"F",@"G",@"A",@"B",nil];
    fancyPitches2   = [[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",nil];
    fancyPitches3   = [[NSMutableArray alloc] initWithObjects:@"F",@"G",@"A",@"B",@"C",@"D",@"E",nil];
    fancyPitches4   = [[NSMutableArray alloc] initWithObjects:@"D",@"E",@"F",@"G",@"A",@"B",@"C",nil];
    fancyPitches5   = [[NSMutableArray alloc] initWithObjects:@"B",@"C",@"D",@"E",@"F",@"G",@"A",nil];
    
    numCorrectAnswers = 0;
    numTotalAnswers   = 0;
    firstAnswer       = true;
    
    switch (level) {
        case 1: {
            tutorialTextArray = [NSMutableArray arrayWithObjects: [NSMutableArray arrayWithObjects:@"These five lines are the staff. They are used in every clef.", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"This is the treble clef:", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"The treble clef wraps around the second line of the staff, which is G line. That's why the treble clef is also called the G clef.", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"A handy way to remember the lines (bottom to top) is Every Good Boy Does Fine: EGBDF!", [NSNumber numberWithInt:1], nil],
                                 [NSMutableArray arrayWithObjects:@"The spaces (from bottom to top) are even easier: FACE.", [NSNumber numberWithInt:1], nil],
                                 [NSMutableArray arrayWithObjects:@"Since the 7 notes of the musical scale range from A to G, the space note under the G line is _?", [NSNumber numberWithInt:kThreeButtons], @"F", nil],
                                 [NSMutableArray arrayWithObjects:@"How about the space note above the G line is _?", [NSNumber numberWithInt:kThreeButtons], @"A", nil],
                                 [NSMutableArray arrayWithObjects:@"It's time to test your knowledge! Correctly answer at least 8/10 of the following questions to complete this level.", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects: [NSString stringWithFormat:@"%@%d%@%d", @"What note is this? ", numCorrectAnswers, @"/10\nRound ", numTotalAnswers + 1],
                                  [NSNumber numberWithInt:kSevenButtons],
                                  [self generateAnswer], nil],
                                 nil];
                        
            break;
        }
        case 2: {
            tutorialTextArray = [NSMutableArray arrayWithObjects: [NSMutableArray arrayWithObjects:@"These five lines are the staff. They are used in every clef.",[NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"This is the bass clef:", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"The bass clef wraps around the second to top line of the staff, which is the F line. That's why the bass clef is also called the F clef.", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"Similar to the treble clef mnemonic, the lines of the bass clef (bottom to top) are Good Boys Do Fine Always: GBDFA.", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"The spaces are All Cows Eat Grass!", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"Since the 7 notes of the musical scale range from A to G, the space note under the F line is _?", [NSNumber numberWithInt:kThreeButtons], @"E", nil],
                                 [NSMutableArray arrayWithObjects:@"How about the space note above the F?", [NSNumber numberWithInt:kThreeButtons], @"G", nil],
                                 [NSMutableArray arrayWithObjects:@"It's time to test your knowledge! Correctly answer at least 8/10 of the following questions to complete this level.", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects: [NSString stringWithFormat:@"%@%d%@%d", @"What note is this? ", numCorrectAnswers, @"/10\nRound ", numTotalAnswers + 1],
                                  [NSNumber numberWithInt:kSevenButtons],
                                  [self generateAnswer], nil],
                                 nil];
                        
            break;
            
        }
        case 3:{
            
            tutorialTextArray = [NSMutableArray arrayWithObjects: [NSMutableArray arrayWithObjects:@"Once again, these five lines are the staff, used with every clef. This is the alto clef:", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"The tenor clef centers around the C line, which is also the middle line of the clef.", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"Again, the 7 notes of the musical scale range from A to G. That means that the space note under the C line is _?", [NSNumber numberWithInt:kThreeButtons], @"B", nil],
                                 [NSMutableArray arrayWithObjects:@"How about the space note above the C?", [NSNumber numberWithInt:kThreeButtons], @"D", nil],
                                 [NSMutableArray arrayWithObjects:@"It's time to test your knowledge! Correctly answer at least 8/10 of the following questions to complete this level.", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects: [NSString stringWithFormat:@"%@%d%@%d", @"What note is this? ", numCorrectAnswers, @"/10\nRound ", numTotalAnswers + 1],
                                  [NSNumber numberWithInt:kSevenButtons],
                                  [self generateAnswer], nil],
                                 nil];
                        
            break;
        }
        case 4:{
            tutorialTextArray = [NSMutableArray arrayWithObjects: [NSMutableArray arrayWithObjects:@"Once again, these five lines are the staff. They are used in every clef.",[NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"This is the tenor clef:", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"Like the alto clef, the tenor clef is shaped to center around the C line. Here, the C line is the second from the top.", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects:@"Since the 7 notes of the musical scale range from A to G, the space note under the C line is _?", [NSNumber numberWithInt:kThreeButtons], @"B", nil],
                                 [NSMutableArray arrayWithObjects:@"How about the space note above the C?", [NSNumber numberWithInt:kThreeButtons], @"D", nil],
                                 [NSMutableArray arrayWithObjects:@"It's time to test your knowledge! Correctly answer at least 8/10 of the following questions to complete this level.", [NSNumber numberWithInt:kTutorial], nil],
                                 [NSMutableArray arrayWithObjects: [NSString stringWithFormat:@"%@%d%@%d", @"What note is this? ", numCorrectAnswers, @"/10\nRound ", numTotalAnswers + 1],
                                  [NSNumber numberWithInt:kSevenButtons],
                                  [self generateAnswer], nil],
                                 nil];
                        
            break;
        }
        case 5: {
            // Start off the medieval clef on the second line
            
            tutorialTextArray = [NSMutableArray arrayWithObjects: [NSMutableArray arrayWithObjects:@"As you hopefully know by now, these five lines are the staff. They are used in every clef.",[NSNumber numberWithInt:kTutorial], @"", [NSNumber numberWithInt:2], [NSNumber numberWithInt: -9], nil],
                                 [NSMutableArray arrayWithObjects:@"This is the medieval C clef! It is no longer used in modern music, but it was used in medieval manuscripts.", @"", [NSNumber numberWithInt:kTutorial], [NSNumber numberWithInt:2], [NSNumber numberWithInt: -9], nil],
                                 [NSMutableArray arrayWithObjects:@"Like alto & tenor, the medieval clef shows where C is by bracketing a line.", [NSNumber numberWithInt:kTutorial], @"", [NSNumber numberWithInt:2], [NSNumber numberWithInt: -9], nil],
                                 [NSMutableArray arrayWithObjects:@"This clef, however, can move! Here, it's on the second line, so that's C.", [NSNumber numberWithInt:kTutorial], @"", [NSNumber numberWithInt:2], [NSNumber numberWithInt: -9], nil],
                                 [NSMutableArray arrayWithObjects:@"If that line is C, what's this note?", [NSNumber numberWithInt:kThreeButtons], @"B", [NSNumber numberWithInt:2], [NSNumber numberWithInt:1], nil],
                                 [NSMutableArray arrayWithObjects:@"How about the space note above the C?", [NSNumber numberWithInt:kThreeButtons], @"D", [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil],
                                 
                                 [NSMutableArray arrayWithObjects:@"Great! Now, if we move the clef, the bottom line becomes C.", [NSNumber numberWithInt:kTutorial], @"", [NSNumber numberWithInt:1], [NSNumber numberWithInt: -9], nil],
                                 [NSMutableArray arrayWithObjects:@"If that line is C, what's this note?", [NSNumber numberWithInt:kThreeButtons], @"B", [NSNumber numberWithInt:1], [NSNumber numberWithInt:-1], nil],
                                 [NSMutableArray arrayWithObjects:@"How about the space note above the C?", [NSNumber numberWithInt:kThreeButtons], @"D", [NSNumber numberWithInt:1], [NSNumber numberWithInt:1], nil],
                                 
                                 [NSMutableArray arrayWithObjects:@"Wonderful! What if the clef is on the top line?", [NSNumber numberWithInt:kTutorial], @"", [NSNumber numberWithInt:5], [NSNumber numberWithInt: -9], nil],
                                 [NSMutableArray arrayWithObjects:@"That means this note is __", [NSNumber numberWithInt:kThreeButtons], @"B", [NSNumber numberWithInt:5], [NSNumber numberWithInt:7], nil],
                                 [NSMutableArray arrayWithObjects:@"How about the space note above the top line?", [NSNumber numberWithInt:kThreeButtons], @"D", [NSNumber numberWithInt:5], [NSNumber numberWithInt:9], nil],
                                 
                                 [NSMutableArray arrayWithObjects:@"Great job! Remember, the medieval C clef can bracket any of these 5 lines, and that line will be C. Ready to be quizzed?",[NSNumber numberWithInt:kTutorial], @"", [NSNumber numberWithInt:5], [NSNumber numberWithInt: -9], nil],
                                 [NSMutableArray arrayWithObjects: [NSString stringWithFormat:@"%@%d%@%d", @"What note is this? ", numCorrectAnswers, @"/10\nRound ", numTotalAnswers + 1],
                                  [NSNumber numberWithInt:kSevenButtons], [self generateAnswer], [NSNumber numberWithInt: -9], [NSNumber numberWithInt: -9], nil],
                                 nil];
            
            break;
        }
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    /** States:
     1 = text + "Next" button
     2 = text + 3 letter choices
     3 = text + 7 letter choices
     **/
    
    
    buttons = [[NSMutableArray alloc] init];
    
    topView = [[UIView alloc] initWithFrame: CGRectMake(0,0, self.view.frame.size.width, 3 *self.view.frame.size.height/5)];
    topView.backgroundColor = [[UIColor alloc] initWithRed:0.6 green:0.7 blue:0.9 alpha:1.0];
    [self.view addSubview:topView];
    
    self.staffView = [[StaffView alloc] initWithFrame: CGRectMake(0, topView.frame.size.height, self.view.frame.size.width, 2*self.view.frame.size.height/5)];
    
    staffView.layer.borderColor = [UIColor blackColor].CGColor;
    staffView.backgroundColor = [[UIColor alloc] initWithRed:1 green:1 blue:1 alpha:1.0];
    staffView.layer.borderWidth = 3.0f;
    [self.view addSubview:staffView];
    [staffView changeClef:level];

    if (level == 1) {
        [staffView addNote:kQuarter atPitch: 2];
    }
    if (level == 2) {
        [staffView addNote:kQuarter atPitch: 6];
    }
    if (level == 3) {
        [staffView addNote:kQuarter atPitch: 4];
    }
    if (level == 4) {
        [staffView addNote:kQuarter atPitch: 6];
    }

    
    [staffView addGestureRecognizer:[[UITapGestureRecognizer alloc]
                                     initWithTarget:self action:@selector(clickTracker:)]];
    [staffView setUserInteractionEnabled:YES];
    
    
    tutorialText = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, self.view.frame.size.width - 10, self.view.frame.size.height - 120)];
    tutorialText.backgroundColor = [[UIColor alloc] initWithRed:0.6 green:0.7 blue:0.9 alpha:1.0];
    tutorialText.text = [self displayText:page];
    tutorialText.font = [UIFont fontWithName:@"Times New Roman" size:30];
    tutorialText.editable = NO;
    [topView addSubview:tutorialText];
    
    
    
    nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    nextButton.frame = CGRectMake((self.view.frame.size.width/2) - 30, topView.frame.size.height - 35, 60, 30);
    [nextButton setTitle:@"Next" forState:(UIControlState)UIControlStateNormal];
    [nextButton addTarget:self action:@selector(nextClicked:) forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
    [self.view addSubview:nextButton];
    
}


- (void)nextClicked : (id) sender{
    page++;
    tutorialText.text = [self displayText:page];
    [self updateButtons:page];
}

- (void)checkAnswer : (id) sender{
    
    int currentDisplay = [(NSNumber *) [(NSMutableArray *)[tutorialTextArray objectAtIndex:page] objectAtIndex: kDisplay] intValue];
    
    if(currentDisplay == kThreeButtons) {
        // If you enter the right answer:
        if( [((UIButton *) sender).titleLabel.text isEqualToString: (NSString*) [(NSMutableArray*) [tutorialTextArray objectAtIndex:page] objectAtIndex:2]] )
        {
            page++;
            tutorialText.text = [self displayText:page];
            
            [self updateButtons:page];
            
        }
        
        // If you enter the wrong answer...
        else{
            NSString *combined = [NSString stringWithFormat:@"%@%@", @"Try that again!\n", (NSString*) [(NSMutableArray*) [tutorialTextArray objectAtIndex:page] objectAtIndex:0]];
            tutorialText.text = combined;
        }
        
    }
    
    if(currentDisplay == kSevenButtons) {
        // If you enter the right answer:
        if( [((UIButton *) sender).titleLabel.text isEqualToString: (NSString*) [(NSMutableArray*) [tutorialTextArray objectAtIndex:page] objectAtIndex:kAnswer]] )
        {
            page++;
            numTotalAnswers++;
            if(firstAnswer) {
                numCorrectAnswers++;
            }
            
            firstAnswer = true;
            if(numTotalAnswers == kNumAllowedTries) {
                if (numCorrectAnswers >= kNumCorrectAnswersNeeded) {
                    [tutorialTextArray addObject: [NSMutableArray arrayWithObjects: [NSString stringWithFormat:@"%@%d%@", @"Awesome job! You did it! ", numCorrectAnswers, @"/10!"], [NSNumber numberWithInt:kWin], [NSNumber numberWithInt: -9], [NSNumber numberWithInt: -9], [NSNumber numberWithInt: -9], nil]];
                    
                    for(UIButton *uib in buttons){
                        [uib removeFromSuperview];
                    }
                    [buttons removeAllObjects];
                    UIButton *startOver = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                    startOver.frame = CGRectMake((self.view.frame.size.width/2) - 50, self.view.frame.size.height - 35, 100, 30);
                    [startOver setTitle:@"Next Level:" forState:(UIControlState)UIControlStateNormal];
                    [startOver addTarget:self action:@selector(backToLevels:) forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
                    [self.view addSubview:startOver];
                    
                    if (levelsBeat < level)
                        levelsBeat++;
                    
                }
                else {
                    [tutorialTextArray addObject: [NSMutableArray arrayWithObjects: [NSString stringWithFormat:@"%@%d%@", @"Awww dudee.. :(", numCorrectAnswers, @"/10..."], [NSNumber numberWithInt:kLose], [NSNumber numberWithInt: -9], [NSNumber numberWithInt: -9], [NSNumber numberWithInt: -9], nil]];                }
            }
            
            else {
                [tutorialTextArray addObject: [NSMutableArray arrayWithObjects: [NSString stringWithFormat:@"%@%d%@%d", @"What note is this? ", numCorrectAnswers, @"/10\nRound ", numTotalAnswers + 1],
                                               [NSNumber numberWithInt:kSevenButtons],
                                               [self generateAnswer], [NSNumber numberWithInt: -9], [NSNumber numberWithInt: -9],  nil]];
            }
            tutorialText.text = [self displayText:page];
            
            [self updateButtons:page];
        }
        
        // If you enter the wrong answer...
        else{
            NSString *combined = [NSString stringWithFormat:@"%@%@", @"Try that again!\n", (NSString*) [(NSMutableArray*) [tutorialTextArray objectAtIndex:page] objectAtIndex:0]];
            tutorialText.text = combined;
            firstAnswer = false;
        }
    }
}

- (NSString*)displayText: (int) pageNum
{
    
    if(pageNum >= tutorialTextArray.count){
        return @"";
    }
    else{
        return (NSString*) [(NSMutableArray*) [tutorialTextArray objectAtIndex:pageNum] objectAtIndex:0];
        
    }
}

- (void) backToLevels: (id) sender
{
    AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    [appDelegate.navController pushViewController: [[LevelSelectViewController alloc] init] animated:YES];
    
}

- (void)updateButtons: (int) stateNum
{
    if(page < tutorialTextArray.count){
        
        switch (level) {
                
            case 5:
                
            {
                int staffPos = [(NSNumber *) [ (NSMutableArray *) [tutorialTextArray objectAtIndex:page] objectAtIndex: kStaffPos] intValue];
                if(staffPos == 1) {
                    staffView.currentClef.frame = CGRectMake(0, 30.5, 80, 110);
                }
                else if (staffPos == 2) {
                    staffView.currentClef.frame = CGRectMake(0, 14.5, 80, 110);
                }
                else if (staffPos == 3) {
                    staffView.currentClef.frame = CGRectMake(0, -2.5, 80, 110);
                }
                else if (staffPos == 4) {
                    staffView.currentClef.frame = CGRectMake(0, -18.5, 80, 110);
                }
                else if (staffPos == 5) {
                    staffView.currentClef.frame = CGRectMake(0, -34.5, 80, 110);
                }
                
                NSNumber *notePos = (NSNumber *) [ (NSMutableArray *) [tutorialTextArray objectAtIndex:page] objectAtIndex: kNotePos];
                if ([notePos intValue] > -9) {
                    [staffView addOtherNote: [notePos intValue]];
                }
                
            }
            default: {
                NSNumber *newNumber = [[tutorialTextArray objectAtIndex: stateNum] objectAtIndex: 1];
                
                if([newNumber intValue] == kTutorial) {
                    for(UIButton __strong * uib in buttons){
                        [uib removeFromSuperview];
                    }
                    [buttons removeAllObjects];
                    
                    [self.view addSubview:nextButton];
                    [buttons addObject:nextButton];
                }
                
                // A screen with 3 choices
                if([newNumber intValue] == kThreeButtons){
                    
                    for(UIButton __strong * uib in buttons){
                        [uib removeFromSuperview];
                    }
                    [buttons removeAllObjects];
                    
                    [buttons addObject: (button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect])];
                    [buttons addObject: (button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect])];
                    [buttons addObject: (button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect])];
                    
                    for(UIButton __strong * uib in buttons){
                        [uib addTarget:self action:@selector(checkAnswer:) forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
                        [self.view addSubview:uib];
                    }
                    
                    button1.frame = CGRectMake(30, self.view.frame.size.height - 35, 60, 30);
                    button2.frame = CGRectMake(130, self.view.frame.size.height - 35, 60, 30);
                    button3.frame = CGRectMake(230, self.view.frame.size.height - 35, 60, 30);
                    
                    switch (level) {
                            
                        case 1:{
                            [button1 setTitle:@"F" forState:(UIControlState)UIControlStateNormal];
                            
                            [button2 setTitle:@"G" forState:(UIControlState)UIControlStateNormal];
                            
                            [button3 setTitle:@"A" forState:(UIControlState)UIControlStateNormal];
                            
                            break;
                        }
                        case 2:{
                            [button1 setTitle:@"E" forState:(UIControlState)UIControlStateNormal];
                            
                            [button2 setTitle:@"F" forState:(UIControlState)UIControlStateNormal];
                            
                            [button3 setTitle:@"G" forState:(UIControlState)UIControlStateNormal];
                            
                            break;
                        }
                        case 3:{
                            [button1 setTitle:@"B" forState:(UIControlState)UIControlStateNormal];
                            
                            [button2 setTitle:@"C" forState:(UIControlState)UIControlStateNormal];
                            
                            [button3 setTitle:@"D" forState:(UIControlState)UIControlStateNormal];
                            break;
                        }
                        case 4:{
                            [button1 setTitle:@"B" forState:(UIControlState)UIControlStateNormal];
                            
                            [button2 setTitle:@"C" forState:(UIControlState)UIControlStateNormal];
                            
                            [button3 setTitle:@"D" forState:(UIControlState)UIControlStateNormal];
                            break;
                        }
                        case 5:{
                            [button1 setTitle:@"B" forState:(UIControlState)UIControlStateNormal];
                            
                            [button2 setTitle:@"C" forState:(UIControlState)UIControlStateNormal];
                            
                            [button3 setTitle:@"D" forState:(UIControlState)UIControlStateNormal];
                            break;
                        }
                            
                    }
                    [nextButton removeFromSuperview];
                }
                
                
                // A screen with all 7 letters as choices
                else if([newNumber intValue] == kSevenButtons){
                    
                    for(UIButton __strong * uib in buttons){
                        [uib removeFromSuperview];
                    }
                    [buttons removeAllObjects];
                    
                    [buttons addObject: (button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect])];
                    [buttons addObject: (button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect])];
                    [buttons addObject: (button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect])];
                    [buttons addObject: (button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect])];
                    [buttons addObject: (button5 = [UIButton buttonWithType:UIButtonTypeRoundedRect])];
                    [buttons addObject: (button6 = [UIButton buttonWithType:UIButtonTypeRoundedRect])];
                    [buttons addObject: (button7 = [UIButton buttonWithType:UIButtonTypeRoundedRect])];
                    
                    for(UIButton __strong * uib in buttons){
                        [uib addTarget:self action:@selector(checkAnswer:) forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
                        [self.view addSubview:uib];
                    }
                    
                    button1.frame = CGRectMake(16, self.view.frame.size.height - 35, 30, 30);
                    [button1 setTitle:@"A" forState:(UIControlState)UIControlStateNormal];
                    
                    button2.frame = CGRectMake(59, self.view.frame.size.height - 35, 30, 30);
                    [button2 setTitle:@"B" forState:(UIControlState)UIControlStateNormal];
                    
                    button3.frame = CGRectMake(102, self.view.frame.size.height - 35, 30, 30);
                    [button3 setTitle:@"C" forState:(UIControlState)UIControlStateNormal];
                    
                    button4.frame = CGRectMake(145, self.view.frame.size.height - 35, 30, 30);
                    [button4 setTitle:@"D" forState:(UIControlState)UIControlStateNormal];
                    
                    button5.frame = CGRectMake(188, self.view.frame.size.height - 35, 30, 30);
                    [button5 setTitle:@"E" forState:(UIControlState)UIControlStateNormal];
                    
                    button6.frame = CGRectMake(231, self.view.frame.size.height - 35, 30, 30);
                    [button6 setTitle:@"F" forState:(UIControlState)UIControlStateNormal];
                    
                    button7.frame = CGRectMake(274, self.view.frame.size.height - 35, 30, 30);
                    [button7 setTitle:@"G" forState:(UIControlState)UIControlStateNormal];
                    
                    [nextButton removeFromSuperview];
                    
                    
                    level == 5 ? [staffView addOtherNote: currentAnswer] : [staffView addNote:kQuarter atPitch:currentAnswer];
                    
                    if (level == 5) {
                        switch (cleffPositionfromGenerateAnswer) {
                            case 0: {
                                staffView.currentClef.frame = CGRectMake(0, 30.5, 80, 110);
                                break;
                            }
                            case 1: {
                                staffView.currentClef.frame = CGRectMake(0, 14.5, 80, 110);
                                break;
                            }
                            case 2: {
                                staffView.currentClef.frame = CGRectMake(0, -2.5, 80, 110);
                                break;
                            }
                            case 3: {
                                staffView.currentClef.frame = CGRectMake(0, -18.5, 80, 110);
                                break;
                            }
                            case 4: {
                                staffView.currentClef.frame = CGRectMake(0, -34.5, 80, 110);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    
}

- (NSString *) generateAnswer {
    
    int r = arc4random() % 7;
    while(r == currentAnswer) {
        r = arc4random() % 7;
    }
    
    NSString * answer = (NSString *) [treblePitches objectAtIndex:r];
    
    if(level == 5) {
        int x = arc4random() % 5;
        cleffPositionfromGenerateAnswer = x;
        switch (x) {
            case 0: {
                currentAnswer = [fancyPitches1 indexOfObject:answer];
                staffView.currentClef.frame = CGRectMake(0, 30.5, 80, 110);
                break;
            }
            case 1: {
                currentAnswer = [fancyPitches2 indexOfObject:answer];
                staffView.currentClef.frame = CGRectMake(0, 14.5, 80, 110);
                break;
            }
            case 2: {
                currentAnswer = [fancyPitches3 indexOfObject:answer];
                staffView.currentClef.frame = CGRectMake(0, -2.5, 80, 110);
                break;
            }
            case 3: {
                currentAnswer = [fancyPitches4 indexOfObject:answer];
                staffView.currentClef.frame = CGRectMake(0, -18.5, 80, 110);
                break;
            }
            case 4: {
                currentAnswer = [fancyPitches5 indexOfObject:answer];
                staffView.currentClef.frame = CGRectMake(0, -34.5, 80, 110);
                break;
            }
           
        }

    }
    
    else {
        switch (level) {
            case kTreble: {
                currentAnswer = [treblePitches indexOfObject:answer];
                break;
            }
            case kBass: {
                currentAnswer = [bassPitches indexOfObject:answer];
                break;
            }
            case kAlto: {
                currentAnswer = [altoPitches indexOfObject:answer];
                break;
            }
            case kTenor: {
                currentAnswer = [tenorPitches indexOfObject:answer];
                break;
            }
        }
    }
    return answer;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void) clickTracker: (id) sender {
    CGPoint point = [sender locationInView:staffView];
    point = point;
}

@end
